package gui;

import helperClasses.Member;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

public class UserInfoPage extends JPanel implements ActionListener{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -6529083223674628952L;
	
	Member currentMember;
	GUI guiObject;
	
	JPanel passwordPanel, addClassPanel, emailPanel, majorPanel;
	JLabel nameLabel, newPasswordLabel, verifyPasswordLabel, majorLabel,
			classNameLabel, classMajorLabel, emailLabel;
	
	JTextField newPasswordTextField, verifyPasswordTextField, majorTextField,
			classNameTextField, classMajorTextField, emailTextField;
	
	JButton updatePasswordButton, backButton, addClassButton, removeClassButton,
			updateEmailButton, updateMajorButton;
	
	JTable table;
	
	JScrollPane scrollPane;
	
	Vector<Vector<Object>> classList;
	
	public void clearFields()
	{
		newPasswordTextField.setText("");
		verifyPasswordTextField.setText("");
		majorTextField.setText("");
		classNameTextField.setText("");
		classMajorTextField.setText("");
		emailTextField.setText("");
		classList.clear();
		table.updateUI();
	}

	public UserInfoPage(GUI guiObject)
	{
		this.guiObject = guiObject;
		setLayout(new GridBagLayout());
		
		//nameLabel = new JLabel("Name", SwingConstants.RIGHT);
		//userNameField = new JTextField();
		
		//Setting preferred size of the textfield to 100 pixels wide by 20 high
		//userNameField.setPreferredSize(new Dimension(200, 20));
		
		//Constraints.setConstraints(0,0,1,1,1,1,GridBagConstraints.WEST,
				//GridBagConstraints.NONE, new Insets(0,0,0,0),0,0);
		
		//add(nameLabel, Constraints.gbc);
		
		createPasswordPanel();
		
		Constraints.setConstraints(0,0,1,1,1,1,GridBagConstraints.NORTHEAST,
				GridBagConstraints.NONE, new Insets(5,0,0,0),0,0);
		
		add(passwordPanel, Constraints.gbc);
		
		createEmailPanel();
		
		Constraints.setConstraints(0,1,1,1,1,1,GridBagConstraints.NORTHEAST,
				GridBagConstraints.NONE, new Insets(5,0,0,0),0,0);
		
		add(emailPanel, Constraints.gbc);
		
		createMajorPanel();
		
		Constraints.setConstraints(0,2,1,1,1,1,GridBagConstraints.NORTHEAST,
				GridBagConstraints.NONE,new Insets(5,0,0,0),0,0);
		
		add(majorPanel, Constraints.gbc);
		
		createAddClassPanel();
		
		Constraints.setConstraints(0,3,1,1,1,1,GridBagConstraints.NORTHEAST,
				GridBagConstraints.NONE, new Insets(5,0,0,0),0,0);
		
		add(addClassPanel, Constraints.gbc);
		
		backButton = new JButton("Back");
		backButton.setActionCommand("userinfoback");
		backButton.addActionListener(guiObject);
		
		Constraints.setConstraints(0,4,1,1,1,1,GridBagConstraints.NORTHEAST,
				GridBagConstraints.NONE, new Insets(5,0,0,0),0,0);
		
		add(backButton, Constraints.gbc);
		
		//Constraints.setConstraints(3,0,1,3,1,1,GridBagConstraints.WEST,
				//GridBagConstraints.BOTH,new Insets(5,5,5,5),0,0);

		//add(new JSeparator(SwingConstants.VERTICAL), Constraints.gbc);
	}
	
	private void createMajorPanel()
	{
		majorPanel = new JPanel();
		majorPanel.setLayout(new GridBagLayout());
		
		Border loweredetchedClass = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
		TitledBorder titleClass = BorderFactory.createTitledBorder(loweredetchedClass, "Major");
		titleClass.setTitleJustification(TitledBorder.RIGHT);
		majorPanel.setBorder(titleClass);
		
		majorLabel = new JLabel("Major", SwingConstants.RIGHT);
		Constraints.setConstraints(0,0,1,1,1,1,GridBagConstraints.WEST,
				GridBagConstraints.NONE, new Insets(0,0,0,0),0,0);
		majorPanel.add(majorLabel, Constraints.gbc);
		
		majorTextField = new JTextField();
		majorTextField.setPreferredSize(new Dimension(298, 20));
		Constraints.setConstraints(1,0,1,1,1,1,GridBagConstraints.WEST,
				GridBagConstraints.NONE, new Insets(0,5,0,0),0,0);
		majorPanel.add(majorTextField, Constraints.gbc);
		
		updateMajorButton = new JButton("Update");
		updateMajorButton.setActionCommand("userinfomajor");
		updateMajorButton.addActionListener(guiObject);
		Constraints.setConstraints(1,1,1,1,1,1,GridBagConstraints.NORTHEAST,
				GridBagConstraints.NONE, new Insets(5,5,0,0),0,0);
		majorPanel.add(updateMajorButton, Constraints.gbc);
		
	}
	
	private void createAddClassPanel()
	{
		addClassPanel = new JPanel();
		addClassPanel.setLayout(new GridBagLayout());
		
		Border loweredetchedClass = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
		TitledBorder titleClass = BorderFactory.createTitledBorder(loweredetchedClass, "Add Class");
		titleClass.setTitleJustification(TitledBorder.RIGHT);
		addClassPanel.setBorder(titleClass);
		
		classNameLabel = new JLabel("Name");
		Constraints.setConstraints(0,0,1,1,1,1,GridBagConstraints.WEST,
				GridBagConstraints.NONE, new Insets(0,0,0,0),0,0);
		addClassPanel.add(classNameLabel, Constraints.gbc);
		
		classNameTextField = new JTextField();
		classNameTextField.setPreferredSize(new Dimension(200, 20));
		Constraints.setConstraints(1,0,1,1,1,1,GridBagConstraints.WEST,
				GridBagConstraints.BOTH, new Insets(0,5,0,0),0,0);
		addClassPanel.add(classNameTextField, Constraints.gbc);

		classMajorLabel = new JLabel("Major");
		Constraints.setConstraints(0,1,1,1,1,1,GridBagConstraints.WEST,
				GridBagConstraints.NONE, new Insets(0,0,0,0),0,0);
		addClassPanel.add(classMajorLabel, Constraints.gbc);
		
		classMajorTextField = new JTextField();
		classMajorTextField.setPreferredSize(new Dimension(200, 20));
		Constraints.setConstraints(1,1,1,1,1,1,GridBagConstraints.WEST,
				GridBagConstraints.BOTH, new Insets(0,5,0,0),0,0);
		addClassPanel.add(classMajorTextField, Constraints.gbc);
		
		Constraints.setConstraints(1,1,2,1,1,1,GridBagConstraints.CENTER,
				GridBagConstraints.NONE, new Insets(5,0,0,0),0,0);
		add(addClassPanel, Constraints.gbc);
		
		JPanel addRemovePanel = new JPanel();
		
		addClassButton = new JButton("Add");
		addClassButton.setActionCommand("userinfoadd");
		addClassButton.addActionListener(guiObject);
		Constraints.setConstraints(0,2,1,1,1,1,GridBagConstraints.NORTHEAST,
				GridBagConstraints.NONE, new Insets(5,0,0,0),0,0);
		addRemovePanel.add(addClassButton);
		
		removeClassButton = new JButton("Remove");
		removeClassButton.setActionCommand("userinforemove");
		removeClassButton.addActionListener(guiObject);
		Constraints.setConstraints(1,2,1,1,1,1,GridBagConstraints.NORTHEAST,
				GridBagConstraints.NONE, new Insets(5,0,0,0),0,0);
		addRemovePanel.add(removeClassButton);
		
		Constraints.setConstraints(1,2,1,1,1,1,GridBagConstraints.NORTHEAST,
				GridBagConstraints.NONE, new Insets(0,0,0,0),0,0);
		addClassPanel.add(addRemovePanel, Constraints.gbc);
		
		createTable();
		
		Constraints.setConstraints(1,3,2,1,1,1,GridBagConstraints.EAST,
				GridBagConstraints.NONE, new Insets(5,0,0,0),0,0);
		
		addClassPanel.add(scrollPane, Constraints.gbc);
	}
	
	private void createEmailPanel()
	{
		emailPanel = new JPanel();
		emailPanel.setLayout(new GridBagLayout());
		
		Border loweredetched = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
		TitledBorder title = BorderFactory.createTitledBorder(loweredetched, "Email");
		title.setTitleJustification(TitledBorder.RIGHT);
		emailPanel.setBorder(title);
		
		emailLabel = new JLabel("Email", SwingConstants.RIGHT);
		Constraints.setConstraints(0,0,1,1,1,1,GridBagConstraints.EAST,
				GridBagConstraints.NONE, new Insets(0,0,0,0),0,0);
		emailPanel.add(emailLabel, Constraints.gbc);
		
		emailTextField = new JTextField();
		emailTextField.setPreferredSize(new Dimension(299, 20));
		Constraints.setConstraints(1,0,1,1,1,1,GridBagConstraints.WEST,
				GridBagConstraints.NONE, new Insets(0,5,0,0),0,0);
		emailPanel.add(emailTextField, Constraints.gbc);
		
		updateEmailButton = new JButton("Update");
		updateEmailButton.setActionCommand("updateemail");
		updateEmailButton.addActionListener(guiObject);
		Constraints.setConstraints(1,1,1,1,1,1,GridBagConstraints.EAST,
				GridBagConstraints.NONE, new Insets(5,5,0,0),0,0);
		emailPanel.add(updateEmailButton, Constraints.gbc);
	}
	
	private void createPasswordPanel()
	{
		passwordPanel = new JPanel();
		passwordPanel.setLayout(new GridBagLayout());
		
		Border loweredetched = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
		TitledBorder title = BorderFactory.createTitledBorder(loweredetched, "New Password");
		title.setTitleJustification(TitledBorder.RIGHT);
		passwordPanel.setBorder(title);
		
		newPasswordLabel = new JLabel("New Password", SwingConstants.RIGHT);
		Constraints.setConstraints(0,0,1,1,1,1,GridBagConstraints.EAST,
				GridBagConstraints.NONE, new Insets(0,0,0,0),0,0);
		passwordPanel.add(newPasswordLabel, Constraints.gbc);
		
		newPasswordTextField = new JPasswordField();
		newPasswordTextField.setPreferredSize(new Dimension(233, 20));
		Constraints.setConstraints(1,0,1,1,1,1,GridBagConstraints.WEST,
				GridBagConstraints.NONE, new Insets(0,5,0,0),0,0);
		passwordPanel.add(newPasswordTextField, Constraints.gbc);
		
		verifyPasswordLabel = new JLabel("Verify Password", SwingConstants.RIGHT);
		Constraints.setConstraints(0,1,1,1,1,1,GridBagConstraints.EAST,
				GridBagConstraints.NONE, new Insets(0,0,0,0),0,0);
		passwordPanel.add(verifyPasswordLabel, Constraints.gbc);
		
		verifyPasswordTextField = new JPasswordField();
		verifyPasswordTextField.setPreferredSize(new Dimension(233, 20));
		Constraints.setConstraints(1,1,1,1,1,1,GridBagConstraints.WEST,
				GridBagConstraints.NONE, new Insets(0,5,0,0),0,0);
		passwordPanel.add(verifyPasswordTextField, Constraints.gbc);
		
		updatePasswordButton = new JButton("Update");
		updatePasswordButton.setActionCommand("updatepassword");
		updatePasswordButton.addActionListener(guiObject);
		Constraints.setConstraints(1,2,1,1,1,1,GridBagConstraints.NORTHEAST,
				GridBagConstraints.NONE, new Insets(5,0,0,0),0,0);
		passwordPanel.add(updatePasswordButton, Constraints.gbc);
		
		
	}
	
	private void createTable()
	{
        Vector<String> columnNames = new Vector<String>();
        columnNames.add("Class Name");
        columnNames.add("Major");

        classList = new Vector<Vector<Object>>();
        
        Vector<Object> rowHolder = new Vector<Object>();
        
        classList.add(rowHolder);

        table = new JTable(classList, columnNames) 
        {
			private static final long serialVersionUID = 1L;

			public boolean isCellEditable(int rowIndex, int colIndex) {
                return false;   //Disallow the editing of any cell
              }
        };
        table.setPreferredScrollableViewportSize(new Dimension(300, 200));
        
        table.setFillsViewportHeight(true);
        //table.setGridColor(new Color(255,255,255));
        table.setShowGrid(false);

        //Create the scroll pane and add the table to it.
        scrollPane = new JScrollPane(table);

        //Add the scroll pane to this panel.
        
        Constraints.setConstraints(0,3,2,1,1,1,GridBagConstraints.NORTH,
				GridBagConstraints.BOTH, new Insets(5,0,5,0),0,0);
        
        add(scrollPane, Constraints.gbc);
    }
	
	public void actionPerformed(ActionEvent e)
	{
		if("search".equalsIgnoreCase(e.getActionCommand()))
		{
			//data.add(guiObject.search(searchTextField.getText()));
	        
			Vector<Vector<Object>> holder = currentMember.classListToVector();
			
			classList.clear();
			classList.addAll(holder);
			
	        table.updateUI();
		}
	}
	
	public void update()
	{
		Vector<Vector<Object>> holder = currentMember.classListToVector();
		classList.clear();
		classList.addAll(holder);
		
		table.updateUI();
	}
	
	public void updateCurrentMember(Member member)
	{
		currentMember = member;
	}
	
	public void updatePage()
	{
		//nameLabel.setText(currentMember.getName());
		if(currentMember.getMajor() == null)
			majorTextField.setText("");
		else
			majorTextField.setText(currentMember.getMajor());
		emailTextField.setText(currentMember.getEmail());
		update();
	}

}
